<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Loader class */
//require APPPATH . "third_party/MX/Controller.php";

class MY_Controller extends CI_Controller {

    /**
     * Holding class instance
     * @var MY_Controller 
     */
    private static $instance = null;

    /**
     * Holding view variables
     * @var array
     */
    public $view_vars = array();

    /**
     * Default layout name
     */
    public $layout_name = 'default';

    /**
     * Disable layout
     * @var boolean
     */
    public $disable_layout = false;

    /**
     * Disable view
     * @var type 
     */
    public $disable_view = false;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();

        $controller_name = ($this->uri->segment(1))?$this->uri->segment(1):'index';
        $method_name = $this->router->fetch_method();
        
        //Load layout config for module
        $this->config->load('layouts', true);

        ($layout_name = $this->config->item($controller_name . '/' . $method_name, 'layouts')) ||
        ($layout_name = $this->config->item($controller_name, 'layouts')) || 
        ($layout_name = $this->config->item('default', 'layouts'));
        
        //If layout is defined
        if ($layout_name)
        {
            $this->layout_name = $layout_name;
        }
        
        //Assign logged in user info to all views
        $this->load->vars(array(
            '_globals' => array(
                'user' => $this->auth->get_user(),
                'controller_name' => $controller_name,
                'method_name' => $method_name,                
            )
        ));

        //Init self instance
        self::$instance = & $this;
    }

    /**
     * Return self instance
     * @return object 
     */
    public static final function get_instance()
    {
        return self::$instance;
    }

    /**
     * Assign value to view
     * @param string $key
     * @param mix $value 
     */
    protected function assign($key, $value)
    {
        $this->view_vars[$key] = $value;
    }

    /**
     * Disable layout
     * @param boolean $disable 
     */
    protected function disable_layout($disable = true)
    {
        $this->disable_layout = $disable;
    }

    /**
     * Disable view
     * @param boolean $disable 
     */
    protected function disable_view($disable = true)
    {
        $this->disable_view = $disable;
    }

    /**
     * Set layout
     * @param string $layout_name 
     */
    public function set_layout($layout_name)
    {
        $this->layout_name = $layout_name;
    }
}